package cn.JHai.controller;

import cn.JHai.dao.FavoritesDao;
import cn.JHai.dao.MusicDao;
import cn.JHai.domain.Favorite;
import cn.JHai.domain.Music;
import cn.JHai.domain.User;
import cn.JHai.service.FavoritesService;
import cn.JHai.service.impl.GetMusicService;
import cn.JHai.service.impl.MusicServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.security.auth.login.LoginException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@Controller
@RequestMapping("favorites")
public class FavoritesController {

    @Autowired
    FavoritesService favoritesService;

    @Autowired
    MusicServiceImp musicServiceImp;

    @RequestMapping("add")
    public void add(String mid,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        Music music = musicServiceImp.find(mid);

        Favorite f = null;
        try {
            boolean in = favoritesService.save(f);
//			boolean de = dao1.(mid);
            if(in) {
                System.out.println("添加成功成功");
            }
            else {
                System.out.println("添加成功失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("del")
    public void del(String mid,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(mid);
        boolean de = favoritesService.delete(mid);
        if(de) {
            System.out.println("删除成功");
        }
        else {
            System.out.println("删除失败");
        }
    }

    @RequestMapping("Play")
    public void Play(String mid,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Music music = musicServiceImp.find(mid);
        ArrayList<Music> arrayList = new ArrayList<Music>();
        arrayList.add(music);
        HttpSession session = request.getSession();
        ArrayList<Music> musicPlayList = (ArrayList<Music>) session.getAttribute("musicPlayList");
        if(musicPlayList==null) {
            session.setAttribute("musicPlayList", arrayList);
        }
        else {
            musicPlayList.addAll(arrayList);
            session.setAttribute("musicPlayList", musicPlayList);
            System.out.println(session.getAttribute("musicPlayList"));
        }
        if(music!=null) {
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
    }

    @RequestMapping("getFavorite")
    public void getFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        System.out.println("111");

        GetMusicService getMusicService = new GetMusicService();
        ArrayList<Favorite> list = new ArrayList<Favorite>();
        try {
            list = getMusicService.getFavorite(user);
        } catch (LoginException e) {
            e.printStackTrace();
        }
        session.setAttribute("listF", list);
        session.setAttribute("count", 1);
        return;
    }


    @RequestMapping("getFavorite2")
    public void getFavorite2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        System.out.println("111");

        GetMusicService getMusicService = new GetMusicService();
        ArrayList<Favorite> list = new ArrayList<Favorite>();
        try {
            list = getMusicService.getFavorite(user);
        } catch (LoginException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        session.setAttribute("listF", list);
        session.setAttribute("count", 1);
        request.getRequestDispatcher("/my.jsp").forward(request, response);
        return;
    }

    @RequestMapping("play")
    public String play(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        ArrayList<Music> arrayList = new ArrayList<Music>();
        for(int i=0;i<5;i++)
        {
            String id = request.getParameter("mid"+i);
            Music music = musicServiceImp.find(id);
            arrayList.add(music);

        }

        HttpSession session = request.getSession();
        session.setAttribute("musicPlayList", arrayList);
        System.out.println("添加成功");
        return "audio";
    }
}
